﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Interfaces.IngresoEgreso
{
    public partial class ModificarIngresoEgreso : System.Web.UI.Page
    {
        
        private int gestion
        {
            set { ViewState["gest"] = value; }
            get { return (int)ViewState["gest"]; }
        }
        private string horas
        {
            set { ViewState["horas"] = value; }
            get { return ViewState["horas"].ToString(); }
        
        }
        private string minutos
        {
            set { ViewState["minutos"] = value; }
            get { return ViewState["minutos"].ToString(); }

        }
        public char tipo
        {
            set { ViewState["tipo"] = value; }
            get { return (char)ViewState["tipo"]; }
        }
        public void Bind()
        {
            DataList1.DataSource = Processes.GestorIngresoEgreso.getValores(gestion,tipo);
            DataList1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = 0;
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            Response.Expires = 0;
            Response.Cache.SetNoStore();
            Response.AppendHeader("Pragma", "no-cache");
            if (!IsPostBack)
            {
                try
                {
                    if (Request["Gestion"] != null && Request["Tipo"] != null)
                    {

                        gestion = Convert.ToInt32(Request["Gestion"]);
                        if (Processes.GestorIngresoEgreso.verificarExistencia(gestion))
                        {
                            tipo = Convert.ToChar(Request["Tipo"]);
                            if (tipo == 'E')
                                Panel1.GroupingText = "Agregar Egreso";
                            DropDownList1.DataSource = Processes.GestorIngresoEgreso.getEventos(gestion, tipo);
                            DropDownList1.DataSourceID = String.Empty;
                            DropDownList1.DataTextField = "Nombre";
                            DropDownList1.DataValueField = "Codigo";
                            DropDownList1.DataBind();
                            Bind();
                        }
                    }
                    else
                        Bind();
                    


                }
                catch (Exception)
                {
                }
            }
            
                Label5.Visible = false;
                Label16.Visible = false;
                try
                {
                    horas = (DropDownList2.SelectedItem).ToString();
                    minutos = (DropDownList3.SelectedItem).ToString();
                }
                catch (Exception)
                { 
                }
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            try
            {  
                Processes.GestorIngresoEgreso.agregarIE(TextBox9.Text,gestion,DropDownList1.SelectedValue.ToString(),TextBox7.Text,TextBox8.Text,TextBox1.Text,horas,minutos);
                Label16.Visible = true;
                Bind();
            }
            catch (Exception ex)
            {
                Label5.Visible = true;
                Label5.Text = ex.Message;
            }
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["BuscarMaster"] != null)
            {
                string res = (string)Session["BuscarMaster"];
                this.MasterPageFile = res;
            }
        }
        protected void Ver_Click(object sender, EventArgs e)
        {
            LinkButton link = sender as LinkButton;
            Response.Redirect("VerIE.aspx?Gestion="+gestion+"&Evento="+link.CommandName);
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            if (tipo == 'I')
            
                Response.Redirect("ModificarIngresoEgreso.aspx?Gestion=" + gestion+ "&Tipo=E");
            
            
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            if (tipo == 'E')
            
                Response.Redirect("ModificarIngresoEgreso.aspx?Gestion=" + gestion + "&Tipo=I");
            
        }
    }
}